package com.ykdl.member.kid.set;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;

import com.lidroid.xutils.BitmapUtils;
import com.sitech.oncon.barcode.core.CreateBarCodeActivity;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.ActivityDetailBean;
import com.ykdl.member.kid.beans.DoctorInfoBean;
import com.ykdl.member.kid.beans.HomeMessageCount;
import com.ykdl.member.kid.beans.IndexBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.gears.BindStoreActivity;
import com.ykdl.member.kid.util.UmShareUtils;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

import android.content.Intent;
import android.opengl.Visibility;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class InvitationActivity extends BaseScreen implements OnClickListener {

	private com.ykdl.member.kid.util.UmShareUtils mUmShare;
	private String comeFrom;
	private TextView more_activity_button, des, title;
	private ActivityDetailBean bean;
	private BitmapUtils bitmapUtil;
	private ImageView img;
	private String share_pic_url = "";
	private String erwei_codeString="";
	private RelativeLayout layout_has_activity;
	private String activity_id="";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTopTitle("邀请有礼", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		}, 0, 0, null, R.drawable.title_bg, false);
		setContent(R.layout.activity_invitation);
		mUmShare = new UmShareUtils(this);
		bitmapUtil = new BitmapUtils(mContext);
		bitmapUtil.configDefaultLoadingImage(R.drawable.unlonding_icon);
		bitmapUtil.configDefaultLoadFailedImage(R.drawable.unlonding_icon);
		activity_id = this.getIntent().getStringExtra(KidAction.ACTIVITY_ID);
		initView();
		getData();
	}

	private void getData() {
		// TODO Auto-generated method stub
		showProgress("正在加载中,请稍等...");
		String pageurl="";
		if(null==activity_id){
			pageurl = KidConfig.ACTIVITY_DETAIL + "/" + 0;
		}else{
			pageurl = KidConfig.ACTIVITY_DETAIL + "/" + activity_id;
			more_activity_button.setVisibility(View.GONE);
		}
		HttpBaseRequest request = Wxxr.getInstance().getBaseRequest(pageurl);
		TaskManager.startHttpRequest(request, new getDetailTag(),
				ActivityDetailBean.class);
	}

	class getDetailTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			if (t instanceof ActivityDetailBean) {
				finishProgress();
				bean = (ActivityDetailBean) t;
				if(bean.getError()==null){
					layout_has_activity.setVisibility(View.VISIBLE);
					hidErrorTip();
					if (bean.getDetails_pic() != null) {
						bitmapUtil.display(img, bean.getDetails_pic().getUrl());
					}
					if (bean.getNote() != null) {
						des.setText(bean.getNote());
					}

					if (bean.getShare_pic() != null) {
						share_pic_url = bean.getShare_pic().getUrl();
					}
					title.setText(bean.getTitle());
					erwei_codeString=bean.getInvitation_url();
				}else{
					layout_has_activity.setVisibility(View.GONE);
					showErrorTip("更多活动敬请期待！", "");
					Toast.makeText(mContext, bean.getDesc(), 0).show();
				}
				
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "失败", 0).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub

		}

	}

	private void initView() {
		// TODO Auto-generated method stub
		findViewById(R.id.erwei_code).setOnClickListener(this);
		findViewById(R.id.friend_circle).setOnClickListener(this);
		findViewById(R.id.weixin).setOnClickListener(this);
		layout_has_activity=(RelativeLayout) findViewById(R.id.layout_has_activity);
		layout_has_activity.setVisibility(View.GONE);
		img = (ImageView) findViewById(R.id.img);
		des = (TextView) findViewById(R.id.des);
		title = (TextView) findViewById(R.id.title);
		more_activity_button = (TextView) findViewById(R.id.more_activity_button);
		more_activity_button.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.erwei_code:
			Intent intent1 = new Intent(this, CreateBarCodeActivity.class);
			intent1.putExtra(KidAction.BARCODE, erwei_codeString);
			startActivity(intent1);
			break;
		case R.id.weixin:
			mUmShare.MyWeiXinShare(bean);
			break;
		case R.id.friend_circle:
			mUmShare.MyCircleShare(bean);
			break;
		case R.id.more_activity_button:
			Intent intent2 = new Intent(this, InvitationListActivity.class);//
			startActivity(intent2);
			break;
		default:
			break;
		}
	}
}
